package org.spring.springboot.app.agent.withdraw.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.agent.withdraw.domain.po.AccountWithdrawPO;
import org.spring.springboot.app.agent.withdraw.domain.vo.AccountWithdrawShowVO;
import org.spring.springboot.app.agent.withdraw.domain.vo.AccountWithdrawVO;
import org.spring.springboot.app.agent.withdraw.domain.vo.SeachWithdrawVO;
import org.spring.springboot.app.agent.withdraw.service.IAccountWithdrawService;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(description="账号提现管理",tags = ApiIndex.AccountWithdrawController)
@RequestMapping(value = "/api/withdraw")
@RestController
public class AccountWithdrawController {

    @Autowired
    private IAccountWithdrawService accountWithdrawService;


    @ApiOperation(value="查询提现信息列表")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.GET)
    public R<PageInfo<List<AccountWithdrawShowVO>>> findAll(
            @ApiParam(value = "银行卡号", required = false) @RequestParam(defaultValue = "") String banknum,
            @ApiParam(value = "持卡人姓名", required = false) @RequestParam(defaultValue = "") String cardUserName,
            @ApiParam(value = "联系电话", required = false) @RequestParam(defaultValue = "") String phone,
            @ApiParam(value = "当前状态", required = false) @RequestParam(defaultValue = "") String state,
            @ApiParam(value = "用户ID（查询自己的提现 和所属类型为2不能共存）", required = false) @RequestParam(defaultValue = "") String userId,
            @ApiParam(value = "代理商ID（查询自己的提现  和所属类型为1不能共存）", required = false) @RequestParam(defaultValue = "") String officeId,
            @ApiParam(value = "所属类型（1.用户 2.代理商 ）", required = false) @RequestParam(defaultValue = "") String for_type,
            @ApiParam(value = "排序字段(需要与数据库字段对应)", required = false) @RequestParam(defaultValue = "") String sortField,
            @ApiParam(value = "排序方式（ascend正序,descend倒序）", required = false) @RequestParam(defaultValue = "") String sortOrder,
            @ApiParam(value = "开始创建时间（例如1993-08-11 或者 1993-08-11 00:00:00）", required = false) @RequestParam(defaultValue = "") String startCreateDate,
            @ApiParam(value = "结束创建时间（例如1993-08-11 或者 1993-08-11 23:59:59）", required = false) @RequestParam(defaultValue = "") String endCreateDate,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize
    ){
        SeachWithdrawVO vo = new SeachWithdrawVO();
        vo.setBanknum(banknum);
        vo.setCardUserName(cardUserName);
        vo.setUserId(userId);
        vo.setOfficeId(officeId);
        vo.setPhone(phone);
        vo.setState(state);
        vo.setFor_type(for_type);
        vo.setSortFild(sortField);
        vo.setSortOrder(sortOrder);
        vo.setPageNum(pageNum);
        vo.setPageSize(pageSize);
        if(StringUtils.isNotBlank(startCreateDate)){
            if(startCreateDate.indexOf(":")==-1){
                startCreateDate=startCreateDate+" 00:00:00";
            }
        }
        if(StringUtils.isNotBlank(endCreateDate)){
            if(endCreateDate.indexOf(":")==-1){
                endCreateDate=endCreateDate+" 23:59:59";
            }
        }
        vo.setStartCreateDate(startCreateDate);
        vo.setEndCreateDate(endCreateDate);
        R<PageInfo<List<AccountWithdrawShowVO>>> result = new R();
        List<AccountWithdrawPO> _list = accountWithdrawService.findAll(vo);
        PageInfo p = new PageInfo(_list);
        p.setList(getSysUserVOS(p.getList()));
        result.setData(p);
        return result;
    }

    private List<AccountWithdrawShowVO> getSysUserVOS(List<AccountWithdrawPO> _list) {
        List<AccountWithdrawShowVO> list = new ArrayList<AccountWithdrawShowVO>();
        if (_list != null && _list.size()>0) {
            for (AccountWithdrawPO po : _list) {
                AccountWithdrawShowVO vo = new AccountWithdrawShowVO();
                BeanUtils.copyProperties(po,vo);
                vo.setCreate_date(vo.getCreateDate());
                list.add(vo);
            }
        }
        return list;
    }


    @ApiOperation(value="根据ID得到提现信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public R<AccountWithdrawShowVO> find(@ApiParam(value = "用户Id") @RequestParam(defaultValue = "") String id) {
        R<AccountWithdrawShowVO> r = new R<AccountWithdrawShowVO>();
        AccountWithdrawPO po = accountWithdrawService.find(id);
        AccountWithdrawShowVO vo = new AccountWithdrawShowVO();
        BeanUtils.copyProperties(po,vo);
        r.setData(vo);
        return r;
    }

    /**
     * 添加
     */
    @ApiOperation(value="添加提现信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(@ApiParam(value = "提现对象", required = true)@RequestBody AccountWithdrawVO vo) {
        R result =  accountWithdrawService.save(vo);
        return result;
    }


    /**
     * 修改
     */
    @ApiOperation(value="修改提现信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public R update(@ApiParam(value = "提现对象", required = true) @RequestBody AccountWithdrawVO vo) {
        R result =  accountWithdrawService.update(vo);
        return result;
    }

}
